

package com.hazelcast.client.impl.protocol.codec.custom;

import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.Generated;
import com.hazelcast.client.impl.protocol.codec.builtin.CodecUtil;
import com.hazelcast.client.impl.protocol.codec.builtin.DataCodec;
import com.hazelcast.client.impl.protocol.codec.builtin.StringCodec;

import static com.hazelcast.client.impl.protocol.ClientMessage.BEGIN_FRAME;
import static com.hazelcast.client.impl.protocol.ClientMessage.END_FRAME;
import static com.hazelcast.client.impl.protocol.codec.builtin.CodecUtil.fastForwardToEndFrame;
import static com.hazelcast.client.impl.protocol.codec.builtin.FixedSizeTypesCodec.*;

@SuppressWarnings("unused")
@Generated("8d7748112022f3a1d1c147bcda868c21")
public final class ListenerConfigHolderCodec {
    private static final int LISTENER_TYPE_FIELD_OFFSET = 0;
    private static final int INCLUDE_VALUE_FIELD_OFFSET = LISTENER_TYPE_FIELD_OFFSET + INT_SIZE_IN_BYTES;
    private static final int LOCAL_FIELD_OFFSET = INCLUDE_VALUE_FIELD_OFFSET + BOOLEAN_SIZE_IN_BYTES;
    private static final int INITIAL_FRAME_SIZE = LOCAL_FIELD_OFFSET + BOOLEAN_SIZE_IN_BYTES;

    private ListenerConfigHolderCodec() {
    }

    public static void encode(ClientMessage clientMessage, com.hazelcast.client.impl.protocol.task.dynamicconfig.ListenerConfigHolder listenerConfigHolder) {
        clientMessage.add(BEGIN_FRAME.copy());

        ClientMessage.Frame initialFrame = new ClientMessage.Frame(new byte[INITIAL_FRAME_SIZE]);
        encodeInt(initialFrame.content, LISTENER_TYPE_FIELD_OFFSET, listenerConfigHolder.getListenerType());
        encodeBoolean(initialFrame.content, INCLUDE_VALUE_FIELD_OFFSET, listenerConfigHolder.isIncludeValue());
        encodeBoolean(initialFrame.content, LOCAL_FIELD_OFFSET, listenerConfigHolder.isLocal());
        clientMessage.add(initialFrame);

        CodecUtil.encodeNullable(clientMessage, listenerConfigHolder.getListenerImplementation(), DataCodec::encode);
        CodecUtil.encodeNullable(clientMessage, listenerConfigHolder.getClassName(), StringCodec::encode);

        clientMessage.add(END_FRAME.copy());
    }

    public static com.hazelcast.client.impl.protocol.task.dynamicconfig.ListenerConfigHolder decode(ClientMessage.ForwardFrameIterator iterator) {
        // begin frame
        iterator.next();

        ClientMessage.Frame initialFrame = iterator.next();
        int listenerType = decodeInt(initialFrame.content, LISTENER_TYPE_FIELD_OFFSET);
        boolean includeValue = decodeBoolean(initialFrame.content, INCLUDE_VALUE_FIELD_OFFSET);
        boolean local = decodeBoolean(initialFrame.content, LOCAL_FIELD_OFFSET);

        com.hazelcast.internal.serialization.Data listenerImplementation = CodecUtil.decodeNullable(iterator, DataCodec::decode);
        java.lang.String className = CodecUtil.decodeNullable(iterator, StringCodec::decode);

        fastForwardToEndFrame(iterator);

        return new com.hazelcast.client.impl.protocol.task.dynamicconfig.ListenerConfigHolder(listenerType, listenerImplementation, className, includeValue, local);
    }
}
